/**
 * 
 */
package com.gr.staffpm.widget.dialog.feedback;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.datatypes.TaskComment;
import com.gr.staffpm.datatypes.TaskQueue;
import com.gr.staffpm.gof.command.IAjaxCommand;
import com.gr.staffpm.notes.activity.service.IActivityDaoService;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.tasks.service.TaskQueueService;
import com.gr.staffpm.tasks.service.TaskService;


/**
 * @author Graham Rhodes 18 Feb 2011 22:40:33
 */
public class AjaxRejectDialog extends ModalWindow {

    @SpringBean(name = "userService")
    private UserService userService;
    @SpringBean(name = "taskService")
    private TaskService taskService;
    @SpringBean(name = "taskQueueService")
    private TaskQueueService taskQueueService;
    @SpringBean(name = "activityService")
    private IActivityDaoService activityDaoService;
    private static final long serialVersionUID = 1L;
    private int queuedTaskIndex = 0;
    private final IAjaxCommand command;
    private TaskRejectPanel rejectPanel = null;
    private List<IModel<TaskQueue>> selectedQueuedTaskModels = new ArrayList<IModel<TaskQueue>>();

    /**
     * @param id
     */
    public AjaxRejectDialog(String id, IAjaxCommand command) {
        super(id);
        this.command = command;

        setInitialHeight(250);
        setInitialWidth(600);

        setTitle("Please Leave A Reason For Rejection.");
    }

    public void initialise(AjaxRequestTarget target) {
        queuedTaskIndex = 0;
        if (rejectPanel == null) {
            rejectPanel = new TaskRejectPanel(this.getContentId(), selectedQueuedTaskModels.get(0).getObject().getTask()) {

                private static final long serialVersionUID = 1L;

                @Override
                public void onSubmit(AjaxRequestTarget target, String rejectReason, Task task) {
                    AjaxRejectDialog.this.updateTask(rejectReason, task);
                    AjaxRejectDialog.this.onSubmit(target);
                }

                @Override
                public void onCancel(AjaxRequestTarget target) {
                    AjaxRejectDialog.this.onSubmit(target);
                }

            };
            setContent(rejectPanel);
        } else {
            rejectPanel.update(target, selectedQueuedTaskModels.get(0).getObject().getTask());
        }
    }

    /**
     * @param rating
     * @param task
     */
    protected void updateTask(String rejectReason, Task task) {

        TaskComment taskComment = new TaskComment();
        taskComment.setComment("Resolution Unapproved.\n\nReason: " + rejectReason);
        taskComment.setCreated(Calendar.getInstance().getTime());
        taskComment.setCreatedBy(userService.getCurrentUser());
        taskComment.setTask(task);
        taskComment.setUpdatedBy(userService.getCurrentUser());
        taskComment.setLastUpdated(Calendar.getInstance().getTime());
        taskService.addComment(taskComment);

        task.setQueued(false);
        task.setUpdatedBy(userService.getCurrentUser());
        task.setLastUpdated(Calendar.getInstance().getTime());
        taskQueueService.removeTaskFromQueue(task);
        taskService.updateTask(task);
        if (!userService.getCurrentUser().equals(task.getAssignee())) {
            activityDaoService.notifyAssigneeOfRejection(task, userService.getCurrentUser());
        }
    }

    public void getRejectReason(AjaxRequestTarget target, Task task) {
        rejectPanel.update(target, task);
    }

    public void onSubmit(AjaxRequestTarget target) {
        queuedTaskIndex++;
        if (queuedTaskIndex < selectedQueuedTaskModels.size()) {
            getRejectReason(target, selectedQueuedTaskModels.get(queuedTaskIndex).getObject().getTask());
        } else {
            command.execute(target);
            close(target);
        }
    }

    public void setQueuedTasks(List<IModel<TaskQueue>> selectedQueuedTaskModels) {
        this.selectedQueuedTaskModels = selectedQueuedTaskModels;
    }
}
